package com.uroad.liaoningpos.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.uroad.liaoningpos.R;
import com.uroad.liaoningpos.common.BaseActivity;
import com.uroad.liaoningpos.common.SPContants;
import com.uroad.liaoningpos.model.VersionMDL;
import com.uroad.liaoningpos.util.SPUtils;
import com.uroad.liaoningpos.util.SystemUtil;
import com.uroad.liaoningpos.webService.PosWS;
import com.uroad.lib.json.FastJsonUtils;
import com.uroad.lib.widget.dialog.ComfirmDialog;
import com.uroad.lib.widget.dialog.DialogHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @创建者 wlw
 * @创建时间 2019/4/2 16:27
 * @描述 用户资料
 */
public class UserInfoActivity extends BaseActivity {
    @BindView(R.id.ivHeadPic)
    ImageView ivHeadPic;
    @BindView(R.id.tvUserName)
    TextView  tvUserName;
    @BindView(R.id.tvNumber)
    TextView  tvNumber;
    @BindView(R.id.tvModifyPwd)
    TextView tvModifyPwd;
    @BindView(R.id.llVersion)
    LinearLayout llVersion;
    @BindView(R.id.tvVersion)
    TextView tvVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentLayout(R.layout.activity_user_info);
        setTitle("个人设置");
        ButterKnife.bind(this);
        setData();
    }

    private void setData() {
        String username = (String) SPUtils.get(this, SPContants.SP_USERNAME, "");
        String departmentid = (String) SPUtils.get(this, SPContants.SP_MERCHANTSHOPID, "");

        tvUserName.setText(username);
        tvNumber.setText("商户编号："+departmentid);
        try {
            String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            tvVersion.setText(versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.tvModifyPwd, R.id.llVersion})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvModifyPwd://修改密码
                openActivity(ModifyPwdActivity.class);
                break;
            case R.id.llVersion://更新版本
//                getVersionInfo();
                break;
        }
    }

    private void getVersionInfo() {
        String deviceuuid = (String) SPUtils.get(this, SPContants.SP_DEVICEUUID, "");
        DialogHelper.showLoading(this, "正在获取版本信息");
        //获取版本信息
        doRequest(PosWS.appVersion, PosWS.appVersionParams(deviceuuid), PosWS.appVersion);
    }

    @Override
    public void OnHttpTaskComplete(String result, String id) {
        super.OnHttpTaskComplete(result, id);
        DialogHelper.endLoading();
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (FastJsonUtils.getJsonStatu00(jsonObject)) {
            if (PosWS.appVersion.equalsIgnoreCase(id)) {
                final VersionMDL mdl = FastJsonUtils.parseDataObjectJSON(jsonObject, VersionMDL.class);
                if (mdl != null) {
                    final String version = getVersion();
                    if (SystemUtil.isNeedUpdate(version, mdl.getVersion()) || SystemUtil.isNeedUpdate(version, mdl.getMinversion())) {
                        String content = mdl.getUpdatelog();
                        DialogHelper.showComfirmDialog(this, "发现新版本,是否更新？", content, "取消", "更新", new ComfirmDialog.DialogOnclick() {
                            @Override
                            public void PerDialogclick(Dialog dialog) {
                                if (SystemUtil.isNeedUpdate(version, mdl.getMinversion())) {
                                    //强制升级
                                    // finish();
                                }
                            }
                        }, new ComfirmDialog.DialogOnclick() {
                            @Override
                            public void PerDialogclick(Dialog dialog) {
                                String url = mdl.getDownloadurl();
                                if (TextUtils.isEmpty(url)) {
                                    showShortToast("链接错误，请稍后再试..");
                                } else {
                                    Uri uri = Uri.parse(url);
                                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                    startActivity(intent);
                                }
                            }
                        });
                    }else{
                        showShortToast("当前已是最新版本");
                    }
                }
            }
        }
    }

    public String getVersion() {
        try {
            String version =  (String) SPUtils.get(this, SPContants.SP_APPVERSION, "");
            if (TextUtils.isEmpty(version)) {
                version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            }
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            showShortToast("获取版本号异常");
            return "";
        }
    }
}
